package com.sn.springboot_netty.swagger;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.sql.Timestamp;
import java.util.Date;

@Configuration
@EnableSwagger2 //开启swagger自动配置
public class Swagger2Config {

    private static final String BASE_PACKAGE = "com.sn.springboot_netty";

    @Value("${swagger.enable}")
    private boolean enableSwagger;

    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(enableSwagger)  //生产环境的时候关闭 swagger 比较安全
                .directModelSubstitute(Timestamp.class,Long.class) //类型转换
                .directModelSubstitute(Date.class,Long.class)
                .select()
                .apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE)) //扫描的包路径
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("swagger test")
                .description("swagger test")
                .version("v1.0")
                .build();
    }

}
